package com.caojing.hlm;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.caojing.hlm.cart.CartFragment;
import com.caojing.hlm.category.CategoryFragment;
import com.caojing.hlm.home.HomeFragment;
import com.caojing.hlm.mine.MineFragment;
import com.caojing.hlm.outlets.OutletsFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.main_container)
    FrameLayout mainContainer;
    @Bind(R.id.main_home)
    ImageView mainHome;
    @Bind(R.id.main_category)
    ImageView mainCategory;
    @Bind(R.id.main_outlets)
    ImageView mainOutlets;
    @Bind(R.id.main_cart)
    ImageView mainCart;
    @Bind(R.id.main_mine)
    ImageView mainMine;
    private final int HOME = 0,CATEGORY=1,OUTLETS=2,CART=3,MINE=4;
    private HomeFragment homeFragment;
    private CategoryFragment categoryFragment;
    private OutletsFragment outletsFragment;
    private CartFragment cartFragment;
    private MineFragment mineFragment;
    private FragmentManager manager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {

        manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        homeFragment = new HomeFragment();
        categoryFragment = new CategoryFragment();
        outletsFragment = new OutletsFragment();
        cartFragment= new CartFragment();
        mineFragment = new MineFragment();
        ft.add(R.id.main_container,homeFragment);
        ft.add(R.id.main_container,categoryFragment);
        ft.add(R.id.main_container,outletsFragment);
        ft.add(R.id.main_container,cartFragment);
        ft.add(R.id.main_container,mineFragment);
        ft.commit();

        selectFragment(HOME);
    }

    private void selectFragment(int position) {

        FragmentTransaction ft1 = manager.beginTransaction();
        mainHome.setSelected(false);
        mainCategory.setSelected(false);
        mainOutlets.setSelected(false);
        mainCart.setSelected(false);
        mainMine.setSelected(false);
        ft1.hide(homeFragment).hide(categoryFragment).hide(outletsFragment).hide(cartFragment).hide(mineFragment);
        switch (position){
            case HOME:
                ft1.show(homeFragment);
                mainHome.setSelected(true);
                break;
            case CATEGORY:
                ft1.show(categoryFragment);
                mainCategory.setSelected(true);
                break;
            case OUTLETS:
                ft1.show(outletsFragment);
                mainOutlets.setSelected(true);
                break;
            case CART:
                ft1.show(cartFragment);
                mainCart.setSelected(true);
                break;
            case MINE:
                ft1.show(mineFragment);
                mainMine.setSelected(true);
                break;
        }
        ft1.commit();
    }

    @OnClick({R.id.main_home, R.id.main_category, R.id.main_outlets, R.id.main_cart, R.id.main_mine})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_home:
                selectFragment(HOME);
                break;
            case R.id.main_category:
                selectFragment(CATEGORY);
                break;
            case R.id.main_outlets:
                selectFragment(OUTLETS);
                break;
            case R.id.main_cart:
                selectFragment(CART);
                break;
            case R.id.main_mine:
                selectFragment(MINE);
                break;
        }
    }
}
